package work.shyk.cdp;


import utils.JsonFormatUtil;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * description: <br>
 * createTime: 2019/7/2310:38 <br>
 *
 * @author zzh
 */
public class TestDemo {

    public static List<OrganiseModel> createTree(List<Organise> organiseList) {
        // 第一级组织是组织编号为01-99的
        Map<String, OrganiseModel> data = new HashMap<>();
        List<OrganiseModel> result = organiseList.stream().filter((e) -> e.getOrganiseId().length() == 2).map((e) -> {
            OrganiseModel organiseModel = new OrganiseModel();
            organiseModel.setId(e.getOrganiseId());
            organiseModel.setText(e.getOrganiseName());
            organiseModel.setOpen("false");
            data.put(organiseModel.getId(), organiseModel);
            return organiseModel;
        }).collect(Collectors.toList());
        recusion(organiseList, data, 2);
        return result;
    }

    public static void recusion(List<Organise> organiseList, Map<String, OrganiseModel> upperLevelData, int level) {
        int length = 2 * level;
        int lastLevelLength = length - 2;
        Map<String, OrganiseModel> data = new HashMap<>();
        List<Organise> list = organiseList.stream().filter((e) -> e.getOrganiseId().length() == length).filter((e) -> {
            OrganiseModel parentNode = upperLevelData.get(e.getOrganiseId().substring(0, lastLevelLength));
            if(parentNode != null) {
                List<OrganiseModel> children = parentNode.getChildren();
                if(children == null) {
                    children = new ArrayList<>();
                    parentNode.setChildren(children);
                    parentNode.setOpen("true");
                }
                OrganiseModel organiseModel = new OrganiseModel();
                organiseModel.setId(e.getOrganiseId());
                organiseModel.setText(e.getOrganiseName());
                organiseModel.setOpen("false");
                children.add(organiseModel);
                data.put(organiseModel.getId(), organiseModel);
                return true;
            }
            return false;
        }).collect(Collectors.toList());
        if(list.size() == 0) {
            return ;
        }
        recusion(organiseList, data, level + 1);
    }

    public static List<OrganiseModel> createTree1(List<Organise> organiseList) {
        return  organiseList.stream().filter((e) -> e.getOrganiseId().length() == 2).map((e) -> {
            OrganiseModel organiseModel = new OrganiseModel();
            organiseModel.setId(e.getOrganiseId());
            organiseModel.setText(e.getOrganiseName());
            organiseModel.setOpen("false");
            recusion(organiseList, organiseModel);
            return organiseModel;
        }).collect(Collectors.toList());
    }

    public static void recusion(List<Organise> organiseList, OrganiseModel parentOrganise) {
        String parentOrganiseId = parentOrganise.getId();
        int length = parentOrganiseId.length() + 2;
        List<OrganiseModel> list =
                organiseList
                        .stream()
                        .filter((e) -> e.getOrganiseId().length() == length && e.getOrganiseId().startsWith(parentOrganiseId))
                        .map((e) -> {
                            OrganiseModel organiseModel = new OrganiseModel();
                            organiseModel.setId(e.getOrganiseId());
                            organiseModel.setText(e.getOrganiseName());
                            organiseModel.setOpen("false");
                            recusion(organiseList, organiseModel);
                            return organiseModel;
                        }).collect(Collectors.toList());
        if(list.size() > 0) {
            parentOrganise.setChildren(list);
            parentOrganise.setOpen("true");
        }
    }

    public static void main(String[] args) {
        List<Organise> list = new ArrayList<>();
        list.add(new Organise("01", "研究院", "YJY"));
        list.add(new Organise("0101", "研究院下部门1", "YJYXBM1"));
        list.add(new Organise("0102", "研究院下部门2", "YJYXBM2"));
        list.add(new Organise("010101", "研究院下部门1下部门1", "YJYXBM1XMB1"));
        list.add(new Organise("010102", "研究院下部门1下部门2", "YJYXBM1XBM2"));
        list.add(new Organise("010201", "研究院下部门2下部门1", "YJYXBM2XBM1"));
        list.add(new Organise("02", "智能制造事业部", "ZNZZSYB"));
        list.add(new Organise("0201", "智能制造事业部下部门1", "ZNZZSYBXBM1"));
        list.add(new Organise("0202", "智能制造事业部下部门2", "ZNZZSYBXBM2"));

        List<OrganiseModel> tree = createTree1(list);
        System.out.println(JsonFormatUtil.formatJson(tree.toString()));
        System.out.println(tree.size());
    }
}
